package com.airlines.base.service.base.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.airlines.base.dao.base.BaseMapper;
import com.airlines.base.service.base.BaseService;
import com.airlines.common.Page;

/**
 * 
 * @author xxxx
 * @date 2016年7月12日
 * @company xxxx Airlines
 * @description 通用service的实现类 只要是在对应的Mapper中实现了由mybatis自动实现的6个增删改查方法，
 * 				 都可以在自定义的ServiceImpl中继承该类
 */
public abstract class BaseServiceImpl<T, ID> implements BaseService<T, ID> {

	private BaseMapper<T, ID> baseMapper;

	public void setBaseMapper(BaseMapper<T, ID> baseMapper) {
		this.baseMapper = baseMapper;
}

	@Override
	public int deleteByPrimaryKey(ID id) {
		return baseMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int save(T record) {
		return baseMapper.insert(record);
	}

	@Override
	public int saveSelective(T record) {
		return baseMapper.insertSelective(record);
	}

	@Override
	public T getByPrimaryKey(ID id) {
		return baseMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(T record) {
		return baseMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(T record) {
		return baseMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<T> listData(T record) {
		return baseMapper.selectByQuery(record);
	}

	@Override
	public Page listData(T record, Page page) {
		PageHelper.startPage(page.getPageNumber(), page.getPageSize());
		List<T> list = baseMapper.selectByQuery(record);
		PageInfo<T> pageInfo = new PageInfo<T>(list);
		page.setTotal(String.valueOf(pageInfo.getTotal()));
		page.setRows(pageInfo.getList());
		return page;
	}
	
	@Override
	public Page getPageFromRequest(HttpServletRequest request){
		String offset = request.getParameter("offset");
		String pageSize = request.getParameter("limit");
		Page page = new Page();
		page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(pageSize))));
		page.setPageSize(Integer.valueOf(pageSize));
		return page;
	}
	
}
